<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>选课案例</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        body{
            padding: 20px;
            width: 100vw;
            display: flex;
            box-sizing: border-box;
        }
        div{
            border: solid 10px #41793f;
            padding: 10px;
            flex: 1;
            background: #16a085;

        }
        div:last-of-type{
            margin-left: -2px;
        }
        ul{
            list-style: none;
            display: flex;
            width: 200px;
            flex-direction: column;
            background: #bccac7;
        }
        li{
            height: 30px;
            border: solid 5px #e67e22;
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-left: 10px;
            color: #333333;
            transition: 1s;
        }
        a{
            border-radius: 3px;
            width: 20px;
            height: 20px;
            text-decoration: none;
            text-align: center;
            background: #16a085;
            color: white;
            cursor: pointer;
            display: flex;
            justify-content: center;
            align-items: center;
            margin-right: 5px;
        }
        .remove{
            border: solid 2px #eeeeee;
            opacity: 0.8;
            color: #eeeeee;
        }
        .remove a{
            background: #eeeeee;
        }
        p{
            margin-top: 20px;
        }
        p span{
            display: inline-block;
            background: #0fc728;
            padding: 5px;
            color: white;
            margin-right: 10px;
            border-radius: 5px;
            margin-bottom: 10px;
        }
        strong{
            color: #eeeeee;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <div>
        <ul>
            <li><span>php</span><a href="javascript:;">+</a></li>
            <li><span>js</span><a href="javascript:;">+</a></li>
            <li><span>vue</span><a href="javascript:;">+</a></li>
            <li><span>java</span><a href="javascript:;">+</a></li>
            <li><span>python</span><a href="javascript:;">+</a></li>
            <li><span>ruby</span><a href="javascript:;">+</a></li>
            <li><span>html</span><a href="javascript:;">+</a></li>
            <li><span>css</span><a href="javascript:;">+</a></li>
        </ul>
    </div>
    <div>
        <strong id="count">请选择你喜欢的课程</strong>
        <p id="lists">
            
        </p>
    </div>
</body>
</html>
<script>
    class Lesson{
        constructor(){
            //初始化数据 将所有需要的数据进行初始化
            this.lis = document.querySelectorAll('ul>li');
            this.countElem = document.getElementById('count');
            this.listElem = document.getElementById('lists');
            this.map = new WeakMap();
        }
        run(){
            this.lis.forEach(item => {
                item.querySelector("a").addEventListener("click",event=>{
                    //获取a元素节点
                    const elem = event.target;
                    const state = elem.getAttribute("select");
                    //证明已经添加过此元素，然后要进行删除等操作
                    if(state){
                        elem.removeAttribute('select');
                        this.map.delete(elem.parentElement);
                        elem.innerHTML = '+';
                        elem.style.backgroundColor = 'green';
                    }else{
                        //证明还没有添加过元素，再次点击需要进行添加操作
                        elem.setAttribute("select",true);
                        this.map.set(elem.parentElement,true);
                        elem.innerHTML = '-';
                        elem.style.backgroundColor = 'red';
                    }
                    //当节点操作完成之后，需要渲染右边的内容
                    this.render();
                });
            });
        }

        count(){
            //只要是在map节点中的，都是现在存在的课程
            return [...this.lis].reduce((count,item) => {
                return count += this.map.has(item) ? 1 : 0;                
            },0);
        }

        lists(){
            //过滤现在存在于map对象中的list然后显示出来
           return [...this.lis].filter(item => {
                return this.map.has(item);
           }).map(item => {
               return `<span>${item.querySelector('span').innerHTML}</span>`;
           });
        }

        render(){
            //一共选择的课程
            if(this.count()){
                this.countElem.innerHTML = `一共选了${this.count()}门课程`;
            }else{
                this.countElem.innerHTML = '请选择你喜欢的课程';
            }
            //选择的课程列表
            this.listElem.innerHTML = this.lists().join('');
        }
    }

    new Lesson().run();
</script>